<template>
    <div class="company-box">
        <el-dialog
            title="更多详情"
            :visible.sync="is_dialog"
            width="400px"
            top="11vh"
        >
            <div class="dialog-box">
                <div class="list-box comm-list">
                    <div
                        class="list-row"
                        v-for="item in list_data"
                        :key="item.id"
                    >
                        <div class="list-left">
                            <span class="title">{{ item.title }}</span>
                        </div>
                        <div class="list-right">
                            <el-button
                                type="primary"
                                icon="el-icon-edit"
                                circle
                                @click.stop="eidtattreTitle(item, list_data)"
                            ></el-button>
                            <el-switch
                                v-model="item.status"
                                active-color="#3388ff"
                                inactive-color="#dcdfe6"
                                active-value="1"
                                inactive-value="2"
                                @change="setModifychildattre(item, list_data)"
                            >
                            </el-switch>
                        </div>
                    </div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import config from "@/js/conf/Config.js";

export default {
    name: "company",
    props: ["datatype"],
    data() {
        return {
            is_dialog: false,
            list_data: [],
        };
    },
    created() {},
    methods: {
        eidtattreTitle(item, data) {
            this.$prompt("请输入价格名称", "编辑", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                inputPattern: /^.+$/,
                inputErrorMessage: "请输入价格名称",
                inputValue: item.title,
            })
                .then(async ({ value }) => {
                    data.map((v) => {
                        if (v.id == item.id) {
                            v.title = value;
                        }
                    });
                    this.setattreSave(item, data);
                })
                .catch(() => {});
        },
        setModifychildattre(item, data) {
            this.setattreSave(item, data);
        },
        async setattreSave(item, data) {
            console.log(data);
            let url =
                this.datatype == 1
                    ? config.GDPRE_ATTR_SAVE
                    : config.PROPRE_ATTR_SAVEONE;
            let param = {};
            if (this.datatype == 1) {
                param = {
                    setval: data,
                };
            } else {
                param = {
                    id: item.id,
                    title: item.title,
                    status: item.status,
                };
            }

            let res = await this.$api({ url: url, data: param });
            if (res.status == 1) {
                this.getListFun(res.status);
            }
        },
        async getListFun(type = null) {
            let url =
                this.datatype == 1
                    ? config.GDPRE_ATTR_LS
                    : config.PROPRE_ATTR_LS;
            let param = {};
            let res = await this.$api({ url: url, data: param });
            if (
                res.status == 1 &&
                res.data.attr_ls &&
                res.data.attr_ls.length > 0
            ) {
                this.list_data = res.data.attr_ls;
                if (type) {
                    this.$msg({
                        title: "保存成功",
                    });
                }
            }
        },
    },
};
</script>

<style lang="scss" scoped>
.dialog-box {
    .list-box {
        height: 500px;
        overflow: auto;
        margin-top: 10px;
        .list-row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: 50px;
            border-bottom: 1px #f7f7f7 solid;
            padding: 5px 10px;
            cursor: pointer;
            &:hover {
                background: #f7f7f7;
            }
            .list-left {
                .title {
                    max-width: 200px;
                    display: block;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    pointer-events: none;
                }
            }
            ::v-deep.list-right {
                .el-button {
                    padding: 5px;
                    font-size: 12px;
                    vertical-align: bottom;
                    margin-right: 10px;
                }
                .default-attre {
                    border: 1px #459ffd solid;
                    padding: 2px 5px;
                    font-size: 12px;
                    margin-right: 10px;
                    color: #459ffd;
                    .el-icon-check {
                        margin-right: 5px;
                    }
                }
            }
        }
    }
}
</style>